
// 静跳转
function static_jump(object){
	var url = object.attr('href');
	url = url ? url : object.attr('url');
	if($(this).hasClass('no-loading')){
		location.href = url;
		return false;
	}
	jumpurl(url);
	return false;
}

function jumpurl (url, time) {
	var timeout = time > 0 ? time * 1000 : 0;
	if(url){loading();}
	jumpurl_handle = setTimeout(function(){
		clearTimeout(jumpurl_handle);
		if(url){
			location.href = url;
		}else{
			history.go(-1);
		}

	}, timeout);
	removeLoading();
}
function money_format(money) {
    if(isNaN(money)){
        return money;
    }
    money = (Math.round((money - 0) * 100)) / 100;
    money = (money == Math.floor(money)) ? money + ".00" : ((money * 10 == Math.floor(money * 10)) ? money + "0" : money);
    money = String(money);
    var ps = money.split('.');
    var whole = ps[0];
    var sub = ps[1] ? '.' + ps[1] : '.00';
    var r = /(\d+)(\d{3})/;
    while (r.test(whole)) {
        whole = whole.replace(r, '$1' + ',' + '$2');
    }
    money = whole + sub;

    return money;
}
function weixin_event_bind () {
	// 查看图片相册
	$('.preview-image img').unbind();
	$('.preview-image img').click(function () {
		weixin_preview_image(wx, $(this));
	});
	// 查看图片
	$('.view-image').unbind();
	$('.view-image').click(function () {
		weixin_view_image(wx, $(this));
	});
}
function weixin_preview_image (wx, here) {
	var cur_url = here.attr('original');
	var i = 0;
	var urls = [];
	here.parent().parent().find('img').each(function () {
		var url = $(this).attr('original');
		urls[i] = url;
		i++;
	});
	if(i > 0){
		wx.previewImage({
			current: cur_url, // 当前显示图片的http链接
			urls: urls // 需要预览的图片http链接列表
		});
	}
}
function weixin_view_image (wx, here) {
	var pic_url = here.attr('original');
	if(pic_url){
		wx.previewImage({
			current: pic_url, // 当前显示图片的http链接
			urls: [pic_url] // 需要预览的图片http链接列表
		});
	}
}
$(document).ready(function(){
	//加载更多
	$(".load-more").click(function () {
		loading();
		var here    = $(this);
		var url     = here.attr('url');
		var data_to = here.attr('data-to');
			url     = url ? url : here.attr('href');
		if(!url){
			msgbox('URL未配置！');
			return false;
		}
		$.ajax({
			type: "get",
			url: url,
			dataType:"json",
			success: function(data){
				removeLoading();
				console.info(data);
				if(data.code == 1){
					$("." + data_to).append(data.data.html);
					next_url = data.data.page.ajax_next_page;
					if(next_url){
						here.attr('url',next_url);
					}else{
						msgbox('到底了');
						here.remove();
					}
				}
				// 重新绑定微信事件
				weixin_event_bind();
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});

		return false;
	});
	
	//ajax表单提交
	$(document).on('submit', 'form.ajax-form', function () {
		var url         = $(this).attr('action');
		var method      = $(this).attr('method');
		var method      = method ? method : 'post';
		var data        = $(this).serialize();
		var loading_msg = $(this).attr('loading-msg');
		var error 		= false;
		var msg         = '';
		
		$(this).find('input').each(function(){
			if($(this).hasClass('required') && $(this).val() == ''){
				msg   = $(this).attr('data-label') + '不能为空';
				error = true;
				return false;
			}
		});

		if(error){
			msgbox(msg);
			return false;
		}

		loading(loading_msg ? loading_msg : '正在提交，请稍候...');
		$.ajax({
			url : url,
			data : data,
			type : method == 'post' ? 'post' : 'get',
			dataType:'json',
			success : function(json){
				removeLoading();
				if(json.code == 1){
					msgbox(json.msg);
				}else{
					msgbox(json.msg, 'error');
				}
				//有返回链接，则跳转
				if(json.url){
					jumpurl(json.url, 2);
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});
		return false;
	});
	
	// win-box
	$(".my-win").on('click', function(){
		$(this).win();
	});

	//返回按钮处理
	$('.jump').on('click', function () {
		var url = $(this).attr('href');
			url = url ? url : $(this).attr('url');
			url = url ? url : '';
		jumpurl(url);
		return false;
	});

	//操作提示
	$(document).on('click', ".opt-tip", function(){
		var here         = $(this);
		var url          = $(this).attr('url');
			url          = url ? url : $(this).attr('href');
		var tourl        = $(this).attr('jumpurl');
		var del_mod      = $(this).attr('del-class');
		var add_class    = $(this).attr('add-class');
		var remove_class = $(this).attr('remove-class');
		var loading_tip  = $(this).attr('tip-loading');
			loading_tip  = loading_tip ? loading_tip : '正在操作，请稍候...';
			add_class    = add_class ? add_class : 'on';
		var unon_text    = $(this).attr('unon-text');
		var on_text      = $(this).attr('on-text');
		var success_callback = $(this).attr('success-callback');
		var error_callback   = $(this).attr('error-callback');
		var is_return        = $(this).attr('is-return');

		var icon   = $(this).attr('on-icon');
		var noicon = $(this).attr('unon-icon');
		var position = $(this).attr('position');
			position = position ? position : '';
		if (!url) {
			return false;
		}
		loading(loading_tip);
		$.ajax({
			type: "get",
			url: url,
			dataType: "json",
			success : function(data){
				removeLoading();
				if(data.code == 1){
					if(data.msg){
						msgbox(data.msg);
					}
					if(tourl){
						jumpurl(tourl);
					}
					if(data.url){
						jumpurl(data.url);
					}
					if(del_mod){
						$('.' + del_mod).fadeOut('slow', function () {
							$('.' + del_mod).remove();
						});
					}

					if(here.hasClass(add_class)){
						here.removeClass(add_class);
						if(position){
							var html = '' ;
								html += '<i class="' + icon + '"></i>';
								html += '<div class="aui-bar-tab-label">' + unon_text + '</div>';
							here.html(html)
						}else{
							here.text(unon_text);
						}
					}else{
						here.addClass(add_class);
						if(remove_class){here.removeClass(remove_class);}
						if(position){
							var html = '' ;
								html += '<i class="' + icon + '"></i>';
								html += '<div class="aui-bar-tab-label">' + on_text + '</div>';
							here.html(html);
						}else{
							here.text(on_text);
						}
					}

					var node_id     = data.data.node_id;
					var node_data   = data.data.node_data;
					var del_node_id = data.data.del_node_id;
					
					if(node_id && node_data){
						$.each(node_data , function(key, value){
							$('.'+node_id).find('.'+key).html(value);
						});
					}
					if(del_node_id){
						$('.'+del_node_id).remove();
					}
					if(success_callback){
						eval(success_callback);
					}
					
				}else{
					msgbox(data.msg, 'error');
					if(error_callback){
						eval(error_callback);
					}
					if(data.url){
						jumpurl(data.url, 2);
					}
				}
			},
			error :function (data) {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
				if(error_callback){
					eval(error_callback);
				}
			}
		});
		if(!is_return){
			return false;
		}
	});

	//有确认提示的操作
	$(document).on('click', ".btn-confirm-opt", function (){
		var checkbox  = $(this);
		var url       = $(this).attr('url');
		var del_mod   = $(this).attr('del-class');
		var msg       = $(this).attr('msg');
		var method    = $(this).attr('method');
		var label_mod = $(this).attr('label-class');
		var here      = $(this);

		url = url ? url : $(this).attr('href');
		msg = msg ? msg : '您确认要进行此操作吗？';

		html  = '<div style="margin-top:10px;">';
		html += '<div style="text-align:center;margin-top:25px;margin-bottom:25px;font-size:20px;color:#aaa;">' + msg + '</div>';
		html += '<div style="text-align:center;margin-top:8px"><button class="aui-btn btn-default" onclick="$.winRemove()"><i class="icon-error"></i>取消</button> <button class="aui-btn aui-btn-danger confirm" style="background-color:#ff6e00 !important;margin-left:1rem;"><i class="icon-success"></i>确定</button> </div>';
		html += '</div>';
		html += '<style>.box-content{min-height: 5rem !important;} button, .aui-btn{width:40%;padding:10px;}.box-body{width:initial;height:9.5rem;left:10%;right:10%}</style>';
		$.winDisplay(html, '确认操作');

		var height         = $(".box-cover").height();
		var current_height = $(".box-body").height();

		$(".box-body").css({'top' : (height-current_height)/2 + 'px'});

		$('.confirm').bind('click', function () {
			loading();
			$.ajax({
				type: method ? method : 'get',
				url: url,
				dataType: 'json',
				success: function(data){
					$.winRemove();
					removeLoading();
					if(data.code == 1){
						msgbox(data.msg);
						$('.' + del_mod).fadeOut('slow', function () {
							$('.' + del_mod).remove();
						});
						if(checkbox.attr('checked')){
							checkbox.removeAttr('checked');
						}else{
							checkbox.attr('checked', 'checked');
						}
						if(data.url){
							jumpurl(data.url);
						}
						if(data.data.label || data.data.label == 0){
							if(label_mod){
								$('.' + label_mod).text(data.data.label);
							}else{
								here.text(data.data.label);
							}
						}
					}else{
						msgbox(data.msg, 'error');
					}
				},
				error : function () {
					removeLoading();
					msgbox('请求失败，请稍候再试！', 'error');
				}
			});
		})
		return false;
	}); 

	// 提交
	$(document).on('click', ".submit-btn", function(){
		var from_class = $(this).attr('data-from');
		$(from_class).submit();
	});

	// 跳转
	$(document).on('click', ".goto-btn", function(){
		var to_class = $(this).attr('data-to');
		var scroll   = $(to_class).offset().top;
		$("body").scrollTop(scroll);
	});
	// 图片验证码更换
	$(document).on('click', '.img-verify', function () {
		var url = $(this).attr('href') + '?' + Math.random();
		$(this).attr('src', url);
		return false;
	});
	//获取验证码
	$(document).on('click', '.get-smscode', function () {
		var mobile   = $('input[name="mobile"]').val();
		var imgcode  = $('input[name="imgcode"]').val();
		var url      = $(this).attr('url');
		var sms_type = $(this).attr('sms-type');
			sms_type = sms_type ? sms_type : 'register';
		if(!url){
			msgbox('URL未配置！', 'error');
			return false;
		}
		if(!mobile){
			msgbox('手机号不能为空！', 'error');
			return false;
		}
		if(!imgcode){
			msgbox('图片验证码不能为空！', 'error');
			return false;
		}
		loading('获取验证码中...');
		$.ajax({
			type: "get",
			url: url,
			data: 'type=' + sms_type + '&mobile=' + mobile + '&imgcode=' + imgcode,
			dataType: "json",
			success: function(data){
				removeLoading();
				if(data.code == 1){
					$('.get-smscode').hide();
					$('.smscode-time').css('display', 'inline-block').find('span').text(60);
					msgbox(data.msg);
					var wait = $('.smscode-time span');
					var smscode_interval = setInterval(function(){
						var time = wait.text();
							--time;
							wait.text(time);
						if(time == 0) {
							$('.smscode-time').hide();
							$('.get-smscode').show();
							clearInterval(smscode_interval);
						};
					}, 1000);
				}else{
					// 更换验证码
					var url = $('.img-verify').attr('href') + '?' + Math.random();
					$('.img-verify').attr('src', url);
					msgbox(data.msg, 'error');
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败！', 'error');
			}
		});
	});
});